package com.szewec.ps.ui.road.facilities.investigation.task.roadside.facilities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSONObject;
import com.szewec.ps.R;
import com.szewec.ps.base.BaseMediaPickerFragment;
import com.szewec.ps.base.FileUpload;
import com.szewec.ps.databinding.FragmentRoadSideFacilitiesBinding;
import com.szewec.ps.model.road.facilities.investigation.task.RoadFacilitiesInvestigationTask;
import com.szewec.ps.model.road.facilities.investigation.task.RoadFacilitiesInvestigationTaskDTO;
import com.szewec.ps.util.StringUtils;
import com.szewec.ps.util.dialog.LoadingDialog;
import com.szewec.ps.util.imagepicker.adapter.CustomPickerAdapterShowAndEdit;
import com.szewec.ps.util.imagepicker.data.MediaFile;

import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

import static android.app.Activity.RESULT_OK;

public class RoadSideFacilitiesFragment extends BaseMediaPickerFragment {
    private FragmentRoadSideFacilitiesBinding binding;

    // 路段设施调查任务DTO
    private RoadFacilitiesInvestigationTaskDTO.RoadFacilitiesTaskDTO roadFacilitiesTaskDTO;

    // 照片上传数组
    private List<String> photoSketchUploadIdArr = new ArrayList<>();
    // 照片文件数组
    private ArrayList<MediaFile> photoSketchFileArr = new ArrayList<>();
    // 图片适配器
    private CustomPickerAdapterShowAndEdit photoSketchAdapter;
    // 移除的图片ID数组
    private List<String> deletePhotoIdArr = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_road_side_facilities, container, false);
        // 2、获取到视图
        View inflate = binding.getRoot();
        ButterKnife.bind(this, inflate);
        // 设置照片
        initViewFile();
        return inflate;
    }


    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        // 通过Activity中的public方法来获取值
        roadFacilitiesTaskDTO = ((RoadsideFacilitiesActivity) context).pushFragmentData();
    }

    // 向Activity传递删除的文件ID数组对象
    public List<String> getDeletePhotoIdArr() {
        return deletePhotoIdArr;
    }

    // 向Activity传递文件数组对象
    public List<String> getPhotoSketchUploadIdArr() {
        return photoSketchUploadIdArr;
    }

    // 向Activity传递适配器对象
    public CustomPickerAdapterShowAndEdit getPhotoSketchAdapter() {
        return photoSketchAdapter;
    }

    // 设置照片
    private void initViewFile() {
        // 初始化照片适配器
        photoSketchAdapter = initTypeMediaRecyclerView(photoSketchUploadIdArr, photoSketchFileArr,
                binding.photoSketchSketchRV, true, false, true, deletePhotoIdArr);

        RoadFacilitiesInvestigationTask facilitiesInvestigationTask = roadFacilitiesTaskDTO.getFacilitiesInvestigationTask();
        if (facilitiesInvestigationTask == null) {
            return;
        }
        // 先根据fileId查询附件数据
        if (!CollectionUtils.isEmpty(roadFacilitiesTaskDTO.getRoadFacilitiesAlongFileIds())) {
            requestFilesByIds(roadFacilitiesTaskDTO.getRoadFacilitiesAlongFileIds(), this::initAdapterData);
        }
        // 根据业务ID查询附件信息
        else if (StringUtils.isNotEmpty(facilitiesInvestigationTask.getId())) {
            requestFiles(FileUpload.RoadInvestigationFileBizTypeEnum.ROAD_FACILITIES_ALONG_FILE_BIZ_TYPE, facilitiesInvestigationTask.getId(), this::initAdapterData);
        }
    }

    // 往照片适配器里面设置图片显示
    private void initAdapterData(List<MediaFile> mediaFileList) {
        if (CollectionUtils.isEmpty(mediaFileList)) {
            return;
        }
        photoSketchUploadIdArr.clear();
        photoSketchFileArr.clear();
        // 添加到文件数组
        photoSketchFileArr.addAll(mediaFileList);
        for (MediaFile mediaFile : photoSketchFileArr) {
            photoSketchUploadIdArr.add(mediaFile.getId());
        }
        // 设置适配器数据源
        photoSketchAdapter.setmMediaFileList(mediaFileList);
    }

}